package com.cgly.medical.service.impl;

import com.cgly.medical.domain.Drug;
import com.cgly.medical.mapper.DrugMapper;
import com.cgly.medical.service.DrugService;
import com.cgly.medical.util.SnowFlakeGenerateIdWorkerutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-08-19-9:17
 */
@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    private DrugMapper drugMapper;

    @Override
    public List<Drug> getDrugInfoByPage(Drug drug) {
        List<Drug> drugInfoByPage = drugMapper.getDrugInfoByPage(drug);

        return drugInfoByPage;
    }

    @Override
    public int updateDrugInfo(Drug drug) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        drug.setUpdatetime(dateFormat.format(date));
        int i = drugMapper.updateDrugInfo(drug);
        return i;
    }

    @Override
    public Drug selectDrugByid(String id) {
        Drug drug = drugMapper.selectDrugByid(id);
        return drug;
    }

    @Override
    public int deleteDruginfo(String id) {
        int i = drugMapper.deleteDruginfo(id);
        return i;
    }

    @Override
    public int addDruginfo(Drug drug) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//定义格式
        drug.setCreatetime(dateFormat.format(date));
        drug.setUpdatetime(dateFormat.format(date));
        SnowFlakeGenerateIdWorkerutil snowFlakeGenerateIdWorkerutil = new SnowFlakeGenerateIdWorkerutil(0L,0L);
        String id = snowFlakeGenerateIdWorkerutil.generateNextId().substring(0,8);
        Long drugid=Long.valueOf(id);
        drug.setDrugId(drugid);
        int i=drugMapper.addDruginfo(drug);
        return i;
    }
}
